package com.tianyalei.wolf.mddesignlib.view.autorecyclerview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.tianyalei.wolf.mddesignlib.view.autorecyclerview.callback.LoadFinishCallBack;
import com.tianyalei.wolf.mddesignlib.view.autorecyclerview.callback.LoadMoreListener;
import com.tianyalei.wolf.mddesignlib.view.recycledivider.DividerItemDecoration;
import com.zhy.base.adapter.recyclerview.CommonAdapter;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

/**
 * 下拉刷新上拉加载的recyclerView
 */
public class AutoLoadRecyclerView extends RecyclerView implements LoadFinishCallBack {

    private LoadMoreListener loadMoreListener;
    private boolean isLoadingMore;

    public AutoLoadRecyclerView(Context context) {
        this(context, null);
    }

    public AutoLoadRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoLoadRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        isLoadingMore = false;
        //这一步必须有，也可以是Gridview
        setLayoutManager(new LinearLayoutManager(context));
        //设置分隔线
        setDividerDrawable(null);
        //设置滚动监听，来进行加载更多
        addOnScrollListener(new AutoLoadScrollListener());
    }

    public void setLoadMoreListener(LoadMoreListener loadMoreListener) {
        this.loadMoreListener = loadMoreListener;
    }

    /**
     * 设置分隔线,如果不设置，那就用在style里面配置的默认的
     *
     * @param drawable
     */
    public void setDividerDrawable(Drawable drawable) {
        if (drawable == null) {
            addItemDecoration(new DividerItemDecoration(getContext()));
        } else {
            addItemDecoration(new DividerItemDecoration(drawable));
        }
    }

    /**
     * 设置item点击响应
     *
     * @param onItemClickLitener
     */
    public void setOnItemClickLitener(OnItemClickListener onItemClickLitener) {
        ((CommonAdapter) getAdapter()).setOnItemClickListener(onItemClickLitener);
    }

    @Override
    public void loadFinish(Object obj) {
        isLoadingMore = false;
    }

    /**
     * 滑动自动加载监听器
     */
    private class AutoLoadScrollListener extends OnScrollListener {

        //dx The amount of horizontal scroll.
        //dy The amount of vertical scroll.
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            //由于GridLayoutManager是LinearLayoutManager子类，所以也适用
            if (getLayoutManager() instanceof LinearLayoutManager) {
                int lastVisibleItem = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
                int totalItemCount = AutoLoadRecyclerView.this.getAdapter().getItemCount();

                //有回调接口，并且不是加载状态，并且剩下2个item，并且向下滑动，则自动加载
                if (loadMoreListener != null && !isLoadingMore && lastVisibleItem >= totalItemCount -
                        2 && dy > 0) {
                    isLoadingMore = true;
                    loadMoreListener.loadMore();
                }
            }
        }

    }
}
